var express = require("express");
var router = express.Router();


// 导入model.js
var { chatgptModel } = require("../db/chatgpt_model");

/* GET home page. */
router.get("/", function (req, res, next) {
  res.render("index", { title: "Express" });
});

// 添加gpt记录
router.post("/add_gpt", async (req, res) => {
  let body = req.body;
  await chatgptModel.create(body);
  res.send({
    code: 200,
    msg: "添加成功",
  });
});

// 修改gpt记录
router.put("/edit_gpt/:id", async function (req, res) {
  let body = req.body;
  await chatgptModel.updateOne({ _id: req.params.id }, { $set: body });
  res.send({
    code: 200,
    msg: "修改成功",
  });
});

// 删除gpt记录
router.delete("/del_gpt/:id", async function (req, res) {
  await chatgptModel.deleteOne({ _id: req.params.id });
  res.send({
    code: 200,
    msg: "删除成功",
  });
});



router.get("/gpt_list", async (req, res) => {
  console.log(req.query);
  let list = [];
  let total = 0;
  list = await chatgptModel
    .find({cloudID:req.query.cloudID})
    .sort({ times: -1 })
    .exec();
  total = list.length;
  res.send({
    code: 200,
    msg: "展示成功",
    data: list,
    total: total,
  });
});

module.exports = router;
